package cn.iocoder.yudao.module.common.service.knowledgepointrelation;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.common.controller.admin.knowledgepointrelation.vo.*;
import cn.iocoder.yudao.module.common.dal.dataobject.knowledgepointrelation.KnowledgePointRelationDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.common.dal.mysql.knowledgepointrelation.KnowledgePointRelationMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.common.enums.ErrorCodeConstants.*;

/**
 * 知识关系点 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class KnowledgePointRelationServiceImpl implements KnowledgePointRelationService {

    @Resource
    private KnowledgePointRelationMapper knowledgePointRelationMapper;

    @Override
    public Long createKnowledgePointRelation(KnowledgePointRelationSaveReqVO createReqVO) {
        // 插入
        KnowledgePointRelationDO knowledgePointRelation = BeanUtils.toBean(createReqVO, KnowledgePointRelationDO.class);
        knowledgePointRelationMapper.insert(knowledgePointRelation);
        // 返回
        return knowledgePointRelation.getId();
    }

    @Override
    public void updateKnowledgePointRelation(KnowledgePointRelationSaveReqVO updateReqVO) {
        // 校验存在
        validateKnowledgePointRelationExists(updateReqVO.getId());
        // 更新
        KnowledgePointRelationDO updateObj = BeanUtils.toBean(updateReqVO, KnowledgePointRelationDO.class);
        knowledgePointRelationMapper.updateById(updateObj);
    }

    @Override
    public void deleteKnowledgePointRelation(Long id) {
        // 校验存在
        validateKnowledgePointRelationExists(id);
        // 删除
        knowledgePointRelationMapper.deleteById(id);
    }

    private void validateKnowledgePointRelationExists(Long id) {
        if (knowledgePointRelationMapper.selectById(id) == null) {
            throw exception(KNOWLEDGE_POINT_RELATION_NOT_EXISTS);
        }
    }

    @Override
    public KnowledgePointRelationDO getKnowledgePointRelation(Long id) {
        return knowledgePointRelationMapper.selectById(id);
    }

    @Override
    public PageResult<KnowledgePointRelationDO> getKnowledgePointRelationPage(KnowledgePointRelationPageReqVO pageReqVO) {
        return knowledgePointRelationMapper.selectPage(pageReqVO);
    }

}